﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL.Interface;

namespace DrugStorehouseMIS.DAL
{
    public class StockInHeaderDAL:IStockInHeaderDAL
    {
        #region IStockInHeaderDAL 成员

        public IDataReader Select(string StockInNum)
        {
            try
            {
                return SqlDbHelper.RunProcedure_Reader(ConnectionString.ConnString, "Proc_StockInHeader_Select", new SqlParameter[] { new SqlParameter("@StockInNum", StockInNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public DataTable SelectByStatus(string Status)
        {
            try
            {
                return SqlDbHelper.RunProcedure_DataTable(ConnectionString.ConnString, "Proc_StockInHeader_Select_By_Status", new SqlParameter[] { new SqlParameter("@Status", Status) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
            
        }

        public DataTable Select(DateTime StartTime, DateTime EndTime)
        {
            try
            {
                 SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("@StartTime",StartTime),
                    new SqlParameter("@EndTime",EndTime),
                };
                return SqlDbHelper.RunProcedure_DataTable(ConnectionString.ConnString, "Proc_StockInHeader_Filter_By_Time", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public DataTable Select(string StockInNum, string StockInTypeName, string SupplierName, string Status)
        {
            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("@StockInNum",StockInNum),
                    new SqlParameter("@StockInTypeName",StockInTypeName),
                    new SqlParameter("@SupplierName",SupplierName),  
                    new SqlParameter("@Status",Status)
                };
                return SqlDbHelper.RunProcedure_DataTable(ConnectionString.ConnString, "Proc_StockInHeader_Filter", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Insert(string StockInNum, string StockInTypeName, string SupplierName, DateTime StockInDatetime, string CreatedBy, string ApprovalBy, string Remark, string Status)
        {
            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("@StockInNum",StockInNum),
                    new SqlParameter("@StockInTypeName",StockInTypeName),
                    new SqlParameter("@StockInDatetime",StockInDatetime),
                    new SqlParameter("@SupplierName",SupplierName),                  
                    new SqlParameter("@CreatedBy",CreatedBy),
                    new SqlParameter("@ApprovalBy",ApprovalBy),                    
                    new SqlParameter("@Remark",Remark),
                    new SqlParameter("@Status",Status)
                };

                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_StockInHeader_Insert", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Delete(string StockInNum)
        {
            try
            {
                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_StockInHeader_Delete",new SqlParameter[] { new SqlParameter("@StockInNum", StockInNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Update(string StockInNum)
        {
            try
            {
                return SqlDbHelper.RunSQL_Query(ConnectionString.ConnString, "update stock_in_header set status='已审核' where stock_in_num=@StockInNum  ", new SqlParameter[] { new SqlParameter("@StockInNum", StockInNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }           
        }

        public int Update(string StockInNum, string StockInTypeName, string SupplierName, DateTime StockInDatetime, string CreatedBy, string ApprovalBy, string Remark, string Status)
        {
            try
            {
                SqlParameter [] all=new SqlParameter[]
                {
                    new SqlParameter("@StockInNum",StockInNum),
                    new SqlParameter("@StockInTypeName",StockInTypeName),
                    new SqlParameter("@StockInDatetime",StockInDatetime),
                    new SqlParameter("@SupplierName",SupplierName),                  
                    new SqlParameter("@CreatedBy",CreatedBy),
                    new SqlParameter("@ApprovalBy",ApprovalBy),                    
                    new SqlParameter("@Remark",Remark),
                    new SqlParameter("@Status",Status)
                };
                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_StockInHeader_Update", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        #endregion
    }
}
